package module.map.model
{
	import drongo.desktop.undo.UndoSystem;
	import drongo.desktop.utils.FileUtil;
	import drongo.desktop.utils.UtilsFile;
	import drongo.model.propertys.ArrayProperty;
	import drongo.model.propertys.StringProperty;
	import drongo.model.values.ArrayValue;
	import drongo.model.values.CollectionValue;
	
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.filesystem.File;
	import flash.net.FileFilter;
	
	import common.window.AlertDialog;
	
	import fairygui.GRoot;
	
	import robotlegs.bender.framework.api.IContext;

	public class ProjectModel extends EventDispatcher
	{
		/**
		 * 工程文件
		 */
		public var projectFile:File;
		public var projectRootFile:File;
		public var mapsFile:File;
		public var imagesFile:File;
		/**
		 * 工程数据
		 */
		public var project:CollectionValue;
		
		/**
		 * 已打开列表
		 */
		public var openList:ArrayValue;
		
		/**
		 * 当前工作域的UI根节点
		 */
		public var root:GRoot;
		
		/**
		 * 操作系统
		 */
		public var undoSystem:UndoSystem;
		
		public function ProjectModel()
		{
			this.project=new CollectionValue();
			this.openList=new ArrayValue();
		}
		
		/**
		 * 初始化
		 */
		public function Init(projectFile:File,root:GRoot,stage:Stage,context:IContext):void{
			this.root=root;
			this.projectFile=projectFile;
			this.projectRootFile=this.projectFile.parent;
			this.mapsFile=new File(this.projectRootFile.nativePath+File.separator+"maps"+File.separator);
			if(this.mapsFile.exists==false){
				this.mapsFile.createDirectory();
			}
			this.imagesFile=new File(this.mapsFile.nativePath+File.separator+"images"+File.separator);
			if(this.imagesFile.exists==false){
				this.imagesFile.createDirectory();
			}
			this.project.Add(new ArrayProperty(Propertys.MAPS));
			this.undoSystem=new UndoSystem();
			this.undoSystem.startup(stage,context);
		}
		
		/**
		 * 刷新地图列表
		 */
		public function RefreshMapList():void{
			var mapFileList:Array=FileUtil.getDirectoryListing(this.mapsFile,new FileFilter("地图配置","*.json"));
			var isChanged:Boolean;
			if(mapFileList.length>0){
				var file:File;
				var mapData:MapModel;
				var config:String;
				var configData:Object;
				for (var i:int = 0; i < mapFileList.length; i++) 
				{
					file=mapFileList[i];
					mapData=new MapModel();
					config=UtilsFile.loadString(file);
					try
					{
						configData=JSON.parse(config);
						mapData.Decode(0,configData);
						if(!this.maps.containProperty(new StringProperty(Propertys.ID,configData.id))){
							this.maps.Push(mapData);
						}
					} 
					catch(error:Error) 
					{
						AlertDialog.open(this.root,"解析地图数据出错："+file.nativePath);
					}
				}
			}
		}
		
		/**
		 * 当前地图
		 */
		public function get currentMap():MapModel{
			return __currentMap;
		}
		private var __currentMap:MapModel;
		public function set currentMap(value:MapModel):void{
			if(this.__currentMap==value){
				return;
			}
			this.__currentMap=value;	
			this.dispatchEvent(new Event(Event.CHANGE));
		}
		
		/**
		 * 地图列表数据
		 */
		public function get maps():ArrayProperty{
			return this.project.Get(Propertys.MAPS) as ArrayProperty;
		}
		
		private static var instance:ProjectModel;
		public static function get single():ProjectModel{
			return instance;
		}
	}
}